import { defaultProperties } from '@/config/table'

export const getMeterList = (type) => {
  const config = localStorage.getItem(type)
  if (config) {
    return JSON.parse(config)
  } else {
    return useDefault(type)
  }
}

export const setMeterList = (type, newList) => {
  localStorage.setItem(type, JSON.stringify(newList))
}

export const useDefault = (type) => {
  const config = defaultProperties[type] || []
  localStorage.setItem(type, JSON.stringify(config))
  return config
}

export const getLocalCache = () => {
  return {
    crm_clue_table: getMeterList(DICT.CLUE),
    crm_clue_pool_table: getMeterList(DICT.CLUE_POOL),
    crm_customer_table: getMeterList(DICT.CUSTOMER),
    crm_business_table: getMeterList(DICT.BUSINESS),
    crm_contact_table: getMeterList(DICT.CONTACT),
    crm_contract_table: getMeterList(DICT.CONTRACT),
    crm_product_table: getMeterList(DICT.PRODUCT),
    crm_receivable_table: getMeterList(DICT.RECEIVABLE),
    crm_receivable_plan_table: getMeterList(DICT.RECEIVABLE_PLAN),
    crm_customer_pool_table: getMeterList(DICT.CUSTOMER_POOL)
  }
}

export const resetLocalCache = (config) => {
  setMeterList(config['crm_clue_table'])
  setMeterList(config['crm_clue_pool_table'])
  setMeterList(config['crm_customer_table'])
  setMeterList(config['crm_business_table'])
  setMeterList(config['crm_contact_table'])
  setMeterList(config['crm_contract_table'])
  setMeterList(config['crm_product_table'])
  setMeterList(config['crm_receivable_table'])
  setMeterList(config['crm_receivable_plan_table'])
  setMeterList(config['crm_customer_pool_table'])
}

export enum DICT {
  CLUE = 'crm_clue_table',
  CLUE_POOL = 'crm_clue_pool_table',
  CUSTOMER = 'crm_customer_table',
  BUSINESS = 'crm_business_table',
  CONTACT = 'crm_contact_table',
  CONTRACT = 'crm_contract_table',
  PRODUCT = 'crm_product_table',
  RECEIVABLE = 'crm_receivable_table',
  RECEIVABLE_PLAN = 'crm_receivable_plan_table',
  CUSTOMER_POOL = 'crm_customer_pool_table'
}
